package com.xiaoh.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 本地文件写数据
 *
 * @author xiaoH
 * @date 2020/4/29
 * @description
 */
public class NIOFileChannel01 {

    public static void main(String[] args) throws Exception{
        String str = "Hello, 小H";
        //创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("/Users/xiaoh/netty/file01.txt");
        //通过fileOutputStream获取对应的fileChannel
        //这个fileChannel真实的类型是FileChannelImpl
        FileChannel fileChannel = fileOutputStream.getChannel();

        //创建一个缓冲区ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        //将str放入到byteBuffer中
        byteBuffer.put(str.getBytes());

        //对byteBuffer进行flip
        byteBuffer.flip();

        //将byteBuffer写入到fileChannel
        fileChannel.write(byteBuffer);

        fileOutputStream.close();
    }
}
